package org.ediba.siri.web.rest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ediba.siri.core.Filter;
import org.ediba.siri.core.action.domain.ActionDownload;
import org.ediba.siri.core.action.service.ActionService;
import org.ediba.siri.core.publication.domain.Publication;
import org.springframework.web.bind.annotation.ResponseBody;

public class ActionDownloadController extends Controller {
	
	private ActionService actionService;
	public void setActionService(ActionService actionService) {this.actionService = actionService;}
	
	@ResponseBody
	public Map<String,Object> getHandler(HttpServletRequest request, HttpServletResponse response, ActionDownloadCommand command){
		Integer total = 0;
		if (command.getSort() == null)
			command.setSort("image.name");
		if (command.getDir() == null)
			command.setDir(Boolean.TRUE);
		
		List<ActionDownload> actionDownloads = new ArrayList<ActionDownload>();
		
		if (command.getId() != null){//un actionDownload en particular
			//ActionDownload actionDownload = this.actionService.downloadRetrieve(command.getId());
			//actionDownloads.add(actionDownload);
			//total = actionDownload!=null?1:0;
		}else if (command.getIdUser() != null){//los action download de un usuario
			Map<Filter,Object> filter = new HashMap<Filter, Object>();
			filter.put(Filter.USER, command.getIdUser());
			actionDownloads.addAll(this.actionService.downloadRetrieve(filter, command.getSort(), command.getDir(), command.getStart(), command.getLimit()));
			total = this.actionService.downloadRetrieveCount(filter);
		}else{ //Todos los action download paginados y ordenados
			List<ActionDownload> result = this.actionService.downloadRetrieve(command.getSort(), command.getDir(), command.getStart(), command.getLimit());
			actionDownloads.addAll(result);
			total = this.actionService.downloadRetrieveCount();
		}
		Map<String,Object> _result = new HashMap<String,Object>(4);
		_result.put("data", actionDownloads);
		_result.put("total", total);
		_result.put("success", true);
		_result.put("message", "OK");
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> usedHandler(HttpServletRequest request, HttpServletResponse response, ActionDownloadCommand command){
		Integer idDownload = command.getId();
		Integer idPublicationNumber = command.getIdPublicationNumber();
		Integer page = command.getPage();
		Boolean modified = command.getModified();
		modified = modified==null?false:true;
		
		int _error = this.actionService.usedDownload(idDownload, idPublicationNumber, page, modified);
		
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("success", _error==ActionService.ERROR_OK);
		_result.put("message", "OK");
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> unusedHandler(HttpServletRequest request, HttpServletResponse response, ActionDownloadCommand command){
		Integer idDownload = command.getId();
		
		int _error = this.actionService.unusedDownload(idDownload);
		
		Map<String,Object> _result = new HashMap<String,Object>(2);
		_result.put("success", _error==ActionService.ERROR_OK);
		_result.put("message", "OK");
		return _result;
	}
	
	@ResponseBody
	public Map<String,Object> getPublicationsHandler(HttpServletRequest request, HttpServletResponse response, ActionDownloadCommand command){
		Integer idDownload = command.getId();
		
		List<Publication> pubs = this.actionService.publicationsRetrieve(idDownload);
		
		Map<String,Object> _result = new HashMap<String,Object>(4);
		_result.put("data", pubs);
		_result.put("total", pubs.size());
		_result.put("success", true);
		_result.put("message", "OK");
		return _result;
	}
}
